package com.gateway.web.servlet.interceptor;

import com.gateway.api.util.LoginUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @author: shaoyu1
 * @date: 2019/11/13 9:48
 */
public class LoginInterceptor  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //获取请求的URL
        String url = httpServletRequest.getRequestURI();
        //静态资源
        if(url.indexOf("/static/")>=0){
            return true;
        }
        //api路径
        if(url.indexOf("/api/")>=0){
            return true;
        }
        //登陆页面
        if(url.indexOf("/login")>=0){
            return true;
        }
        //登陆到主页
        if(url.indexOf("/main")>=0){
            return true;
        }
        //主页
        if(url.equals("/")){
            return true;
        }
        if(LoginUtil.checkLogin(httpServletRequest)){
            return true;
        }
        //不符合条件的，跳转到登录界面
        httpServletRequest.getRequestDispatcher("/index/login").forward(httpServletRequest, httpServletResponse);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
